﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TaoSH.AppCode;
using System.Data;
using TaoSH.Data;
using Newtonsoft.Json;


namespace TaoSH.View
{
    public partial class CommonList : BasePage
    {
        public string strFormCode
        {
            get { return Request.QueryString["FormCode"] ?? "CJWT"; }
        }



        protected void Page_Load(object sender, EventArgs e)
        {

            JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);
            string strQueryName = "../../View/UCList/" + FormBaseData.FormQueryUrl;
            QueryUControl Control = new QueryUControl();
            if (!string.IsNullOrEmpty(FormBaseData.FormQueryUrl))
            {
                Control = LoadControl(strQueryName) as QueryUControl;
                Control.ID = "UserQueryControl";
                QueryCon.Controls.Add(Control);

                Button btDel = Control.FindControl("DelForm") as Button;
                if (btDel != null)
                {
                    btDel.Click += new EventHandler(this.btDel_Click);

                }

                Button btQuery = Control.FindControl("btQuery") as Button;

                if (btQuery != null)
                {
                    btQuery.Click += new EventHandler(this.btQuery_Click);

                }
            }


            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(FormBaseData.FormQueryUrl))
                {
                    querydata(Control.SetinitQuery() + Control.SetDefaultQuery());
                    hidQueryWhere.Value = Control.SetinitQuery() + Control.SetDefaultQuery();
                }
                else
                {
                    querydata("");
                }
            }
        }


        public void querydata(string strWhere)
        {
            string strReturn = "";
            int intPageLen = int.Parse(hidPageLen.Value);
            int intIndex = int.Parse(ddlPageIndex.SelectedValue);
            int intPageStart = (intIndex - 1) * intPageLen + 1;
            int intPageEnd = intIndex * intPageLen;


            JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);

            string strFormTable = FormBaseData.FormTable;
            if (!string.IsNullOrEmpty(FormBaseData.FormGetSQL))
            {
                strFormTable = " (" + FormBaseData.FormGetSQL + ") as Form ";
            }


            DataTable dtResult = new DataTable();
            dtResult = new JH_Auth_FormBaseB().GetDTByCommand("  select * from ( select row_number() over(order by  ID DESC ) as RowNum, * from  " + strFormTable + " where 1=1 " + strWhere + "  ) t  where   t.RowNum between " + intPageStart + " and " + intPageEnd);
            strReturn = JsonConvert.SerializeObject(dtResult);




            tabledata.Value = strReturn;
            hidDataCount.Value = GetQueryCount(FormBaseData, strWhere);
            double PageCount = Math.Ceiling(double.Parse(hidDataCount.Value) / double.Parse(hidPageLen.Value));

            new CommonHelp.BindData().BindPage(ddlPageIndex, PageCount.ToInt32());
            if (ddlPageIndex.Items.Contains(new ListItem() { Text = intIndex.ToString(), Value = intIndex.ToString() }))
            {
                ddlPageIndex.SelectedValue = intIndex.ToString();
            }
            else
            {
                ddlPageIndex.SelectedValue = "1";
            }


        }

        protected void ddlPageIndex_SelectedIndexChanged(object sender, EventArgs e)
        {
            querydata(hidQueryWhere.Value);
        }

        public string GetQueryCount(JH_Auth_FormBase FormBaseData, string strWhere)
        {
            string strReturn = "";
            string strFormTable = FormBaseData.FormTable;
            if (!string.IsNullOrEmpty(FormBaseData.FormGetSQL))
            {
                strFormTable = " (" + FormBaseData.FormGetSQL + ") as Form ";
            }
            DataTable dt = new DataTable();
            dt = new JH_Auth_FormBaseB().GetDTByCommand("  select count(*) as Datacount from ( select row_number() over(order by  ID DESC ) as RowNum, * from  " + strFormTable + " where 1=1 " + strWhere + "  ) t ");
            strReturn = dt.Rows[0][0].ToString();
            return strReturn;

        }



        void btDel_Click(Object sender, EventArgs e)
        {
            string strFormID = hidselID.Value;
            if (hidselID.Value != "")
            {
                JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);
                CommonHelp.FormHelp FormHelp = new CommonHelp.FormHelp();
                FormHelp.DelForm(int.Parse(strFormID), FormBaseData.FormTable);
                Response.Write("<script language='JavaScript'>alert('删除成功');window.location=document.location.href;</script>");
            }

        }

        void btQuery_Click(Object sender, EventArgs e)
        {
            UCQueryData();
        }


        public void UCQueryData()
        {

            QueryUControl Control = QueryCon.FindControl("UserQueryControl") as QueryUControl;
            string strQuery = Control.SetinitQuery() + Control.GetQuerydata();
            hidQueryWhere.Value = strQuery;
            ddlPageIndex.SelectedValue = "1";//初始化为第一页结果
            hidselID.Value = "";//初始化选择为空
            querydata(strQuery);
        }
    }
}